package com.ebupt.migu.music.effectStatistics.controller;

import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.ExcelUtils;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.common.util.excelSheet.Sheet;
import com.ebupt.migu.music.effectStatistics.service.BannerOpService;
import com.ebupt.migu.music.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zhuar
 * Banner运营统计
 */
@RestController
@RequestMapping("/BannerOpController")
public class BannerOpController {

    @Autowired
    private IDAuthUtils idAuthUtils;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private BannerOpService bannerOpService;

    private static String[] columnsArr = {"executeTime", "dateId", "strategyId", "strategyName", "executeType", "activityName", "groupName", "userGroupSize", "bgPv", "bgUv", "djPv", "djUv"};
    private static String[] headArr = {"策略执行区间", "创建时间", "策略ID", "策略名称", "执行类型", "活动名称", "目标人群", "人群规模", "曝光PV", "曝光UV", "点击PV", "点击UV"};

    /**
     * 分页、条件查询
     * 查询条件： 开始时间、结束时间、策略名称
     *
     * @return
     */
    @PostMapping("/queryMap")
    public ResultObject queryMap() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        return bannerOpService.queryMap(params);
    }

    /**
     * 查询条件： 开始时间、结束时间、策略名称
     * 全量导出
     *
     * @return
     */
    @RequestMapping(value = "/exportMap", produces = MediaType.TEXT_HTML_VALUE)
    public void exportMap() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        List<HashMap<String, Object>> list = bannerOpService.exportMap(params);
        ExcelUtils excel = new ExcelUtils();
        excel.setName("banner_operation").setVersion("2007");//excel 2007's name
        Sheet sheet = new Sheet();
        sheet.setName("banner位运营");//sheet's name
        sheet.setHead(headArr);
        sheet.fillData2(list, columnsArr);
        excel.addSheet(sheet);
        excel.zipAndResponse(response, request);
    }
}
